/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.InvoiceDAO;
import DAO.ProductDAO;
import DAO.ProductcatDAO;
import POJO.Invoice;
import POJO.Invoiceline;
import POJO.Product;
import POJO.Productcat;
import Util.GuiUtil;
import Util.MsgUtil;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.util.*;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Nguyễn Thi Mỷ Diện (1140214)
 */
public class FrmInvoiceSearch extends javax.swing.JInternalFrame {

    /**
     * Creates new form FrmInvoiceSearch
     */
    private org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(this.getClass().getName());
     
    public FrmInvoiceSearch() {
        initComponents();
        tbInvoice.setModel(new InvoiceTableModel(new ArrayList()));
        
        tbInvoiceline.setModel(new InvoicelineTableModel(new ArrayList()));
        
        this.loadActionCheckboxs();
        this.loadForm();
    }

    //<editor-fold defaultstate="collapsed" desc="Load action checkboxs">
    private void loadActionCheckboxs(){
        ActionListener actionListener = new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource().equals(chkCustomer)){
                    chkCustomer.setSelected(chkCustomer.isSelected());
                    txtSCustomer.setEnabled(chkCustomer.isSelected());
                }
                if(e.getSource().equals(chkProduct)){
                    chkProduct.setSelected(chkProduct.isSelected());
                    cboSProductCat.setEnabled(chkProduct.isSelected());
                    cboSProduct.setEnabled(chkProduct.isSelected());
                }
                if(e.getSource().equals(chkWarehouseDateFrom)){
                    chkWarehouseDateFrom.setSelected(chkWarehouseDateFrom.isSelected());
                    dateInputDateFrom.setEnabled(chkWarehouseDateFrom.isSelected());
                }
                if(e.getSource().equals(chkWarehouseDateTo)){
                    chkWarehouseDateTo.setSelected(chkWarehouseDateTo.isSelected());
                    dateInputDateTo.setEnabled(chkWarehouseDateTo.isSelected());
                }
                if(e.getSource().equals(chkWarehouseId)){
                    chkWarehouseId.setSelected(chkWarehouseId.isSelected());
                    txtWarehoseid.setEnabled(chkWarehouseId.isSelected());
                }
            }
            
        };
        
        chkCustomer.addActionListener(actionListener);
        chkProduct.addActionListener(actionListener);
        chkWarehouseDateFrom.addActionListener(actionListener);
        chkWarehouseDateTo.addActionListener(actionListener);
        chkWarehouseId.addActionListener(actionListener);
    }
    //</editor-fold>
    
    private void loadForm(){
        try {
            List<Productcat> listpProductcats = ProductcatDAO.getListProductCats(1);
            GuiUtil.addListCombobox(cboSProductCat, listpProductcats, "getName");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex.fillInStackTrace());
        }
    }
    
    private InvoiceTableModel getInvoiceTableModel(){
        return (InvoiceTableModel) tbInvoice.getModel();
    }
    private InvoicelineTableModel getInvoicelineTableModel(){
        return (InvoicelineTableModel) tbInvoiceline.getModel();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelDieuKienTK = new javax.swing.JPanel();
        chkWarehouseId = new javax.swing.JCheckBox();
        chkWarehouseDateFrom = new javax.swing.JCheckBox();
        dateInputDateFrom = new com.toedter.calendar.JDateChooser();
        dateInputDateTo = new com.toedter.calendar.JDateChooser();
        chkWarehouseDateTo = new javax.swing.JCheckBox();
        txtWarehoseid = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        chkCustomer = new javax.swing.JCheckBox();
        txtSCustomer = new javax.swing.JTextField();
        chkProduct = new javax.swing.JCheckBox();
        cboSProductCat = new javax.swing.JComboBox();
        cboSProduct = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbInvoice = new javax.swing.JTable();
        paginatorSearchInvoice = new GUI.Components.Paginator();
        jPanel5 = new javax.swing.JPanel();
        jScrollPanel3 = new javax.swing.JScrollPane();
        tbInvoiceline = new javax.swing.JTable();

        setTitle("Tìm kiếm hóa đơn");

        jPanelDieuKienTK.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Điều kiện tìm kiếm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        chkWarehouseId.setText("Mã hóa đơn:");

        chkWarehouseDateFrom.setText("Ngày lập hóa đơn từ:");

        dateInputDateFrom.setEnabled(false);

        dateInputDateTo.setEnabled(false);

        chkWarehouseDateTo.setText("Ngày lập hóa đơn đến:");

        txtWarehoseid.setEnabled(false);

        btnSearch.setText("Tìm kiếm");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        chkCustomer.setText("Khách hàng:");

        txtSCustomer.setEnabled(false);

        chkProduct.setText("Sản phẩm:");

        cboSProductCat.setEnabled(false);
        cboSProductCat.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboSProductCatItemStateChanged(evt);
            }
        });

        cboSProduct.setEnabled(false);

        javax.swing.GroupLayout jPanelDieuKienTKLayout = new javax.swing.GroupLayout(jPanelDieuKienTK);
        jPanelDieuKienTK.setLayout(jPanelDieuKienTKLayout);
        jPanelDieuKienTKLayout.setHorizontalGroup(
            jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDieuKienTKLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(321, 321, 321))
            .addGroup(jPanelDieuKienTKLayout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkWarehouseDateTo)
                    .addComponent(chkWarehouseDateFrom)
                    .addComponent(chkWarehouseId))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtWarehoseid)
                    .addComponent(dateInputDateTo, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addComponent(dateInputDateFrom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(48, 48, 48)
                .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkProduct)
                    .addGroup(jPanelDieuKienTKLayout.createSequentialGroup()
                        .addComponent(chkCustomer)
                        .addGap(18, 18, 18)
                        .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cboSProductCat, 0, 173, Short.MAX_VALUE)
                            .addComponent(txtSCustomer)
                            .addComponent(cboSProduct, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(18, 18, Short.MAX_VALUE))
        );
        jPanelDieuKienTKLayout.setVerticalGroup(
            jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDieuKienTKLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(chkCustomer)
                        .addComponent(txtSCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtWarehoseid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(chkWarehouseId)))
                .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelDieuKienTKLayout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(chkWarehouseDateFrom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(dateInputDateFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelDieuKienTKLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboSProductCat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chkProduct))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelDieuKienTKLayout.createSequentialGroup()
                        .addGroup(jPanelDieuKienTKLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dateInputDateTo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(chkWarehouseDateTo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnSearch))
                    .addGroup(jPanelDieuKienTKLayout.createSequentialGroup()
                        .addComponent(cboSProduct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 39, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách hóa đơn", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        tbInvoice.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã hóa đơn", "Ngày lập hóa đơn", "Khách hàng"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbInvoice.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbInvoiceMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbInvoice);
        tbInvoice.getAccessibleContext().setAccessibleName("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Chi tiết hóa đơn", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        tbInvoiceline.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Loại sản phẩm", "Tên sản phẩm", "Số lượng"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbInvoiceline.setEnabled(false);
        jScrollPanel3.setViewportView(tbInvoiceline);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(202, 202, 202)
                        .addComponent(paginatorSearchInvoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanelDieuKienTK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelDieuKienTK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(paginatorSearchInvoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        Map<String,Object> whCdtions = new HashMap<>();
        if(chkCustomer.isSelected()){
            whCdtions.put("customerName", txtSCustomer.getText());
        }else{
             whCdtions.put("customerName", "");
        }
        if(chkProduct.isSelected()){
            if(cboSProductCat.getSelectedItem() instanceof Productcat){
                whCdtions.put("productCatId",
                            ((Productcat)cboSProductCat.getSelectedItem()).getId() );
                if(cboSProduct.getSelectedItem() instanceof Product){
                     whCdtions.put("productId",
                            ((Product)cboSProduct.getSelectedItem()).getId() );
                }
            }else{
                 whCdtions.put("productCatId",0);
                 whCdtions.put("productId","");
            }
        }else{
              whCdtions.put("productCatId",0);
              whCdtions.put("productId","");
        }
        
        if(chkWarehouseId.isSelected()){
            whCdtions.put("id",txtWarehoseid.getText());
        }else{
            whCdtions.put("id","");
        }
        if(chkWarehouseDateFrom.isSelected()){
            whCdtions.put("dateForm",dateInputDateFrom.getDate());
        }else{
            whCdtions.put("dateForm",new Date(0)); // 1-1-1970
        }
        if(chkWarehouseDateTo.isSelected()){
            whCdtions.put("dateTo",dateInputDateTo.getDate());
        }else{
            whCdtions.put("dateTo",new Date()); // date now
        }
        List<Invoice> listInvoices;
        try {
            listInvoices = InvoiceDAO.searchInvoices(whCdtions);
        } catch (Exception ex) {
            log.error(ex.getMessage(),ex.fillInStackTrace());
            MsgUtil.error("Có lỗi trong quá trình tìm kiếm",ex.getMessage());
            return;
        }
        if(listInvoices == null || listInvoices.isEmpty()){
            getInvoiceTableModel().removeAllElement();
            MsgUtil.info("Không tìm thấy kết quả!");
            return;
        }
        
        // reset invoiceline table
        getInvoicelineTableModel().removeAllElement();
        
        InvoiceTableModel invoiceTable = (InvoiceTableModel) tbInvoice.getModel();
        invoiceTable.setList(listInvoices);
    }//GEN-LAST:event_btnSearchActionPerformed

    private void cboSProductCatItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSProductCatItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            Productcat c = (Productcat) evt.getItem();
            try {
                List<Product> listProduct = ProductDAO.getListProducts(c.getId());
                GuiUtil.addListCombobox(cboSProduct, listProduct, "getName");
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex.fillInStackTrace());
            }
        }
    }//GEN-LAST:event_cboSProductCatItemStateChanged

    private void tbInvoiceMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbInvoiceMouseClicked
         String invoiceId = (String) getInvoiceTableModel()
                                      .getValueAt(tbInvoice.getSelectedRow(),0);
        try {
            List<Invoiceline> listInvoicelineTables =
                            InvoiceDAO.getListInvoicelines(invoiceId);
            getInvoicelineTableModel().setList(listInvoicelineTables);
        } catch (Exception ex) {
            log.error(ex.getMessage(),ex.fillInStackTrace());
        }
    }//GEN-LAST:event_tbInvoiceMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cboSProduct;
    private javax.swing.JComboBox cboSProductCat;
    private javax.swing.JCheckBox chkCustomer;
    private javax.swing.JCheckBox chkProduct;
    private javax.swing.JCheckBox chkWarehouseDateFrom;
    private javax.swing.JCheckBox chkWarehouseDateTo;
    private javax.swing.JCheckBox chkWarehouseId;
    private com.toedter.calendar.JDateChooser dateInputDateFrom;
    private com.toedter.calendar.JDateChooser dateInputDateTo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanelDieuKienTK;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPanel3;
    private GUI.Components.Paginator paginatorSearchInvoice;
    private javax.swing.JTable tbInvoice;
    private javax.swing.JTable tbInvoiceline;
    private javax.swing.JTextField txtSCustomer;
    private javax.swing.JTextField txtWarehoseid;
    // End of variables declaration//GEN-END:variables
}

//<editor-fold defaultstate="collapsed" desc="Invoice model table">
class InvoiceTableModel extends AbstractTableModel{
    
    private static final String[] columnNames = {"Mã phiếu", "Ngày nhập", "Khách hàng"};
    
    private List<Invoice> list;
    
    public InvoiceTableModel(List<Invoice> list) {
        this.list = list;
    }
    
    public List<Invoice> getList(){
        return this.list;
    }
    public void setList(List<Invoice> list){
        this.list = list;
         fireTableDataChanged();
    }
    
    public void removeAllElement(){
        this.list.clear();
        fireTableDataChanged();
    }
    
    public void addElement(Invoice e) {
        // Adds the element in the last position in the list
        list.add(e);
        fireTableRowsInserted(list.size()-1, list.size()-1);
    }
    public void removeElement(int rowIndex){
        list.remove(rowIndex);
        fireTableRowsDeleted(rowIndex, rowIndex);
    }
    
    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }
    @Override
    public String getColumnName(int index){
       return columnNames[index];
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex) {
            case 0: return list.get(rowIndex).getId();
            case 1: return list.get(rowIndex).getInvoicedate().toString();
            case 2: return list.get(rowIndex).getCustomer().getName(); 
        }
        return null;
    }
}
//</editor-fold>

//<editor-fold defaultstate="collapsed" desc="Invoiceline model table">
class InvoicelineTableModel  extends AbstractTableModel{
    
    private static final String[] columnNames = {"Loại sản phẩm", "Sản phẩm", "Số lượng"};
    
    private List<Invoiceline> list;
    
    public InvoicelineTableModel(List<Invoiceline> list) {
        this.list = list;
    }
    
    public List<Invoiceline> getList(){
        return this.list;
    }
    public void setList(List<Invoiceline> list){
        this.list = list;
         fireTableDataChanged();
    }
    public void removeAllElement(){
        this.list.clear();
        fireTableDataChanged();
    }
    
    public void addElement(Invoiceline e) {
        // Adds the element in the last position in the list
        list.add(e);
        fireTableRowsInserted(list.size()-1, list.size()-1);
    }
    public void removeElement(int rowIndex){
        list.remove(rowIndex);
        fireTableRowsDeleted(rowIndex, rowIndex);
    }
    
    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }
    @Override
    public String getColumnName(int index){
       return columnNames[index];
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex) {
            case 0: return list.get(rowIndex).getProduct().getProductcat().getName();
            case 1: return list.get(rowIndex).getProduct().getName();
        }
        return null;
    }
}
//</editor-fold>
